package com.yx1996.management.ExcelUtils;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.yx1996.management.Bean.User;
import com.yx1996.management.DBUtils.DBUtils1;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Component
public class DatabaseOperation {
    /**
     * 将表格的信息同步到数据库中
     *
     * @param users
     * @return
     */
    public boolean SynchronizationToDB(ArrayList<User> users) {
        Connection conn = DBUtils1.getConnection();
        PreparedStatement pstat = null;
        CleanDB();
        try {
            for (int i = 0; i < users.size(); i++) {
                String sql = "INSERT INTO excel VALUES (?,?,?,?,?,?)";
                pstat = conn.prepareStatement(sql);
                pstat.setString(1, users.get(i).getId());
                pstat.setString(2, users.get(i).getUsername());
                pstat.setString(3, users.get(i).getPassword());
                pstat.setString(4, users.get(i).getEmail());
                pstat.setString(5, users.get(i).getAdress());
                pstat.setString(6, users.get(i).getPhonenumber());
                pstat.executeUpdate();
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils1.release(conn, pstat, null);
        }
        return false;
    }

    /**
     * 清除数据库中的所有信息
     */
    public static void CleanDB() {
        Connection conn1 = DBUtils1.getConnection();
        PreparedStatement pstat1 = null;
        try {
            String sql = "truncate table excel";
            pstat1 = conn1.prepareStatement(sql);
            pstat1.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils1.release(conn1, pstat1, null);
        }
    }

    /**
     * 将数据库中的信息与Excel表格信息同步
     *
     * @param filepath excel文件路径
     * @param
     * @return
     */
    public boolean SynchronizationToExcel(String filepath) {
        OutputStream out = null;
        List<User> users;
        users = SelectDB();
        try {
            out = new FileOutputStream(filepath, false);
            ExcelWriter writer = EasyExcelFactory.getWriter(out, ExcelTypeEnum.XLS, true);
            Sheet sheet = new Sheet(1, 1, User.class);
            writer.write(users, sheet);
            writer.finish();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 查询数据库中所有数据并封装到user对象中
     *
     * @return
     */
    public static ArrayList<User> SelectDB() {
        ArrayList<User> users = new ArrayList<>();
        Connection conn = DBUtils1.getConnection();
        PreparedStatement pstat = null;
        ResultSet rs = null;
        try {
            String sql = "select * from excel";
            pstat = conn.prepareStatement(sql);
            rs = pstat.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getString(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setEmail(rs.getString(4));
                user.setAdress(rs.getString(5));
                user.setPhonenumber(rs.getString(6));
                users.add(user);
            }
            return users;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils1.release(conn, pstat, rs);
        }
        return null;
    }
}
